package com.hndfsj.app.datav;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hndfsj.framework.utils.properties.PropertiesUtil;

@Controller
@RequestMapping("/lzxc")
public class RoadController extends DataVBaseController {
	static Logger log = LoggerFactory.getLogger(RoadController.class);
	public static final String PROJECT_URL = PropertiesUtil.getString("project.lzxc.url");

	/**
	 * 概况总数
	 * 
	 * @param type
	 *            [es事件总数,cgls车货损失金额,rls路产损失(万元),dt死亡人数] event car-goods-loss
	 *            road-loss death-toll
	 * @return
	 * @throws Exception
	 *             zxj
	 */
	@RequestMapping(value = "/overview/name/value")
	public void overviewNameValue(@RequestParam String type, HttpServletResponse response) {
		// String url =PROJECT_URL+ "/rest/common/statistics/overview/name/value";
		String url = "/overview/name/value";
		String result = getJsonData(url);
		if (StringUtils.isBlank(result) || request(url, 60 * 60 * 1)) {
			// result = parseServerData2JsonString(url);
			result = getData(url);
			putCache(url, result);
		}
		writeJson(response, "[" + JSON.parseObject(result).getString(type) + "]");
	}

	/**
	 * 事故原因排行榜
	 * 
	 * @param type
	 * @param response
	 *            zxj
	 */
	@RequestMapping(value = "/accident/reason")
	public void accidentReason(HttpServletResponse response) {
		// String url =PROJECT_URL+ "/rest/equip/fault/reason";
		String url = "/accident/reason";
		String result = getJsonData(url);
		if (StringUtils.isBlank(result) || request(url, 60 * 60 * 1)) {
			// result = parseServerData2JsonString(url);
			result = getData(url);
			putCache(url, result);
		}
		writeJson(response, JSON.parseArray(result).toJSONString());
	}

	/**
	 * 工作月报
	 * 
	 * @param type
	 *            月报类型
	 * @param response
	 *            zxj
	 */
	@RequestMapping(value = "/work/report")
	public void siteCarFlow(String type, HttpServletResponse response) {
		String url = "/work/report";
		String result = getJsonData(url);
		if (StringUtils.isBlank(result) || request(url, 60 * 60 * 5)) {
			// result =parseServerData2JsonString(url + "?date=" + DateUtils.formatDate(new
			// Date()));
			result = getData(url);
			putCache(url, result);
		}
		if (StringUtils.isNotBlank(type)) {
			JSON.parseArray(result).forEach(object -> {
				JSONObject jsonObject = ((JSONObject) object);
				if (type.equals(jsonObject.getString("name"))) {
					writeJson(response, "[" + jsonObject.toJSONString() + "]");
				}
			});
		} else {
			writeJson(response, result);
		}
	}

	/**
	 * 事故原因占比
	 * 
	 * @param response
	 *            zxj
	 */
	@RequestMapping(value = "/accident/type/reason")
	public void accidentTypeReason( HttpServletResponse response) {
		// String url =PROJECT_URL+ "/rest/common/statistics/accident/type/reason";
		String url = "/accident/type/reason";
		String result = getJsonData(url);
		if (StringUtils.isBlank(result) || request(url, 60 * 60 * 1)) {
			// result = parseServerData2JsonString(url);
			result = getData(url);
			putCache(url, result);
		}
		writeJson(response, JSON.parseArray(result).toJSONString());
	}

	/**
	 * 天气统计
	 * 
	 * @param response
	 *            zxj
	 */
	@RequestMapping(value = "/weather/day/count")
	public void weatherDayCount(HttpServletResponse response) {
		// String url =PROJECT_URL+ "/rest/common/statistics/weather/day/count";
		String url = "/weather/day/count";
		String result = getJsonData(url);
		if (StringUtils.isBlank(result) || request(url, 60 * 60 * 1)) {
			// result = parseServerData2JsonString(url,"respList");
			result = getData(url);
			putCache(url, result);
		}
		writeJson(response, JSON.parseArray(result).toJSONString());
	}
	
	
	/**
	 * 一天发生时段统计  白天/晚上
	 * @param response
	 * zxj
	 */
	@RequestMapping(value = "/day/time/slot")
	public void dayTimeSlot(HttpServletResponse response) {
		// String url =PROJECT_URL+ "/rest/common/statistics/day/time/slot";
		String url = "/day/time/slot";
		String result = getJsonData(url);
		if (StringUtils.isBlank(result) || request(url, 60 * 60 * 1)) {
			// result = parseServerData2JsonString(url,"respList");
			result = getData(url);
			putCache(url, result);
		}
		writeJson(response, JSON.parseArray(result).toJSONString());
	}
	/**
	 * 一天发生车型统计     大型客货, 中型客货, 小型客货
	 * @param response
	 * zxj
	 */
	@RequestMapping(value = "/car/brand")
	public void carCrand(HttpServletResponse response) {
		// String url =PROJECT_URL+ "/rest/common/statistics/car/brand";
		String url = "/car/brand";
		String result = getJsonData(url);
		if (StringUtils.isBlank(result) || request(url, 60 * 60 * 1)) {
			// result = parseServerData2JsonString(url,"respList");
			result = getData(url);
			putCache(url, result);
		}
		writeJson(response, JSON.parseArray(result).toJSONString());
	}
	/**
	 * 一天发生道路段统计     正常路段,软田路段,匝道 
	 * @param response
	 * zxj
	 */
	@RequestMapping(value = "/road/slot/count")
	public void roadSlotCount(HttpServletResponse response) {
		// String url =PROJECT_URL+ "/rest/common/statistics/car/brand";
		String url = "/road/slot/count";
		String result = getJsonData(url);
		if (StringUtils.isBlank(result) || request(url, 60 * 60 * 1)) {
			// result = parseServerData2JsonString(url,"respList");
			result = getData(url);
			putCache(url, result);
		}
		writeJson(response, JSON.parseArray(result).toJSONString());
	}

	/**
	 * 事故半年趋势
	 * 
	 * @param response
	 *            zxj
	 */
	@RequestMapping(value = "/accident/half/year/trend")
	public void repairYearTrend(HttpServletResponse response) {
//		String url = PROJECT_URL + "/rest/common/statistics/accident/half/year/trend";
		String url ="/accident/half/year/trend";
		String result = getJsonData(url);
		if (StringUtils.isBlank(result) || request(url, 60 * 60 * 1)) {
//			result = parseServerData2JsonString(url, "respList");
			result = getData(url);
			putCache(url, result);
		}
		writeJson(response, JSON.parseArray(result).toJSONString());
	}
	/**
	 * 事故密度 一年中所有交通事故月数据
	 * @param response
	 * zxj
	 */
	@RequestMapping(value = "/accident/density")
	public void accidentDensity(HttpServletResponse response) {
//		String url = PROJECT_URL + "/rest/common/statistics/accident/density";
		String url ="/accident/density";
		String result = getJsonData(url);
		if (StringUtils.isBlank(result) || request(url, 60 * 60 * 1)) {
//			result = parseServerData2JsonString(url, "respList");
			result = getData(url);
			putCache(url, result);
		}
		writeJson(response, JSON.parseArray(result).toJSONString());
	}
	/**
	 * 白天夜晚的比值
	 * @param response
	 * zxj
	 */
	@RequestMapping(value = "/day/night/rate")
	public void dayNightRate(HttpServletResponse response) {
//		String url = PROJECT_URL + "/rest/common/statistics/accident/density";
		String url ="/day/night/rate";
		String result = getJsonData(url);
		if (StringUtils.isBlank(result) || request(url, 60 * 60 * 1)) {
//			result = parseServerData2JsonString(url, "respList");
			result = getData(url);
			putCache(url, result);
		}
		writeJson(response, JSON.parseArray(result).toJSONString());
	}
	@RequestMapping(value = "/immediate/message")
	public void immediateMessage(HttpServletResponse response) {
//		String url = PROJECT_URL + "/rest/common/statistics/accident/density";
		String url ="/immediate/message";
		String result = getJsonData(url);
		if (StringUtils.isBlank(result) || request(url, 60 * 60 * 1)) {
//			result = parseServerData2JsonString(url, "respList");
			result = getData(url);
			putCache(url, result);
		}
		int length=JSON.parseArray(result).size();
		int index=new DateTime().getSecondOfMinute()%length;
		writeJson(response, "["+JSON.parseArray(result).getString(index)+"]");
	}
}
